package com.dingwei.superiordiary.mvp.presenter;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.MyCollectPostsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by lihao on 2017/9/12.
 */

public class MyCollectionPostsPresenter extends basePresenter {

    private Contract.MyCollectionPostsView myCollectionView;

    public MyCollectionPostsPresenter(Contract.MyCollectionPostsView view) {
        super(view);
        this.myCollectionView = view;
    }

    public void initData(int page, String start_time, String end_time, final boolean isRefresh, boolean isShowDiaolg) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.myCollectionList);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("page", page + "");
        if (!start_time.equals("0")) {
            rp.addBodyParameter("start_time", start_time);
            rp.addBodyParameter("end_time", end_time);
        }
        x.http().post(rp, new myCallback(BaseActivity.context, MyCollectPostsBean.class, isShowDiaolg, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                MyCollectPostsBean bean = (MyCollectPostsBean) baseBean;
                if (isRefresh) {
                    myCollectionView.onRefresh(bean);
                } else {
                    myCollectionView.onLoadMore(bean);
                }
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                myCollectionView.onFail();
            }
        });
    }

    public void delete(String id, final int position) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.ActivityCollect);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("action", "0");
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                myCollectionView.deleteSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
}
